﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	internal class HYRevitExchange
	{
		public static HYPipeCal CreatePipe(Pipe revitPipe)
		{
			HYPipeCal hypipeCal = new HYPipeCal();
			hypipeCal.Id = revitPipe.Id.IntegerValue;
			hypipeCal.Number = "RS_" + RevitUtils.GetRevitNumber(revitPipe);
			hypipeCal.Length = RevitUtils.GetCurveLength(revitPipe, BuiltInParameter.CURVE_ELEM_LENGTH);
			hypipeCal.Velocity = RevitUtils.GetVelocity(revitPipe, BuiltInParameter.RBS_PIPE_VELOCITY_PARAM);
			hypipeCal.Flow = RevitUtils.GetFlow(revitPipe, BuiltInParameter.RBS_PIPE_FLOW_PARAM);
            hypipeCal.nDN = (int)Math.Ceiling(RevitUtils.GetSharaParameter(revitPipe, BuiltInParameter.RBS_PIPE_DIAMETER_PARAM, (DisplayUnitType)2));
            hypipeCal.dGrad = RevitUtils.GetSharaParameter(revitPipe, BuiltInParameter.RBS_PIPE_SLOPE, (DisplayUnitType)19);
			hypipeCal.bIsHorL = !YJKRevitTools.isRiserPiping(revitPipe);
			if (hypipeCal.bIsHorL && hypipeCal.dGrad >= 1000.0)
			{
				hypipeCal.bIsHorL = false;
			}
			if (!hypipeCal.bIsHorL)
			{
				hypipeCal.dGrad = 0.0;
			}
			double pipeLevelElevation = RevitUtils.GetPipeLevelElevation(revitPipe);
			hypipeCal.ButtonElevation = RevitUtils.GetDistance(revitPipe, BuiltInParameter.RBS_START_OFFSET_PARAM) + pipeLevelElevation;
			double num = RevitUtils.GetDistance(revitPipe, BuiltInParameter.RBS_END_OFFSET_PARAM) + pipeLevelElevation;
			if (num < hypipeCal.ButtonElevation)
			{
				hypipeCal.ButtonElevation = num;
			}
			return hypipeCal;
		}

		public static HYPipeCal CreatePipe(FlexPipe revitPipe)
		{
			HYPipeCal hypipeCal = new HYPipeCal();
			hypipeCal.Id = revitPipe.Id.IntegerValue;
			hypipeCal.Number = "RF_" + RevitUtils.GetRevitNumber(revitPipe);
			hypipeCal.Length = RevitUtils.GetCurveLength(revitPipe, BuiltInParameter.CURVE_ELEM_LENGTH);
			hypipeCal.Velocity = RevitUtils.GetVelocity(revitPipe, BuiltInParameter.RBS_PIPE_VELOCITY_PARAM);
			hypipeCal.Flow = RevitUtils.GetFlow(revitPipe, BuiltInParameter.RBS_PIPE_FLOW_PARAM);
            hypipeCal.nDN = int.Parse(RevitUtils.GetSharaParameter(revitPipe, BuiltInParameter.RBS_PIPE_DIAMETER_PARAM, (DisplayUnitType)2).ToString());
            hypipeCal.dGrad = RevitUtils.GetSharaParameter(revitPipe, BuiltInParameter.RBS_PIPE_SLOPE, (DisplayUnitType)19);
			hypipeCal.bIsHorL = !YJKRevitTools.isRiserPiping(revitPipe);
			if (hypipeCal.bIsHorL && hypipeCal.dGrad >= 1000.0)
			{
				hypipeCal.bIsHorL = false;
			}
			if (!hypipeCal.bIsHorL)
			{
				hypipeCal.dGrad = 0.0;
			}
			double pipeLevelElevation = RevitUtils.GetPipeLevelElevation(revitPipe);
			hypipeCal.ButtonElevation = RevitUtils.GetDistance(revitPipe, BuiltInParameter.RBS_START_OFFSET_PARAM) + pipeLevelElevation;
			double num = RevitUtils.GetDistance(revitPipe, BuiltInParameter.RBS_END_OFFSET_PARAM) + pipeLevelElevation;
			if (num < hypipeCal.ButtonElevation)
			{
				hypipeCal.ButtonElevation = num;
			}
			return hypipeCal;
		}

		public static HYPipeValveCal CreatePipeValve(FamilyInstance fi)
		{
			if (fi.Symbol.Family.Name.Contains("地漏") || fi.Symbol.Family.Name.Contains("清扫口") || fi.Symbol.Name.Contains("地漏") || fi.Symbol.Name.Contains("清扫口"))
			{
				HYBathroom hybathroom = new HYBathroom();
				hybathroom.Id = fi.Id.IntegerValue;
				hybathroom.szName = (hybathroom.Name = RevitUtils.GetFamilyInstanceName(fi));
                hybathroom.nOutDN = int.Parse(RevitUtils.GetSharaParameter(fi, "出口直径", (DisplayUnitType)2).ToString());
				hybathroom.bDefinition = true;
				hybathroom.dFluxEquivalent = 0.0;
				hybathroom.dFluxFlow = 0.0;
				hybathroom.dSupplyEquivalent = 0.0;
				hybathroom.dSupplyFlow = 0.0;
				return hybathroom;
			}
			if (fi.GetParameter("CWFU") == null)
			{
				return new HYPipeValveCal
				{
					Id = fi.Id.IntegerValue,
                    dResistance = RevitUtils.GetPressure(fi, (BuiltInParameter)(-1140205)),
					Name = RevitUtils.GetFamilyInstanceName(fi)
				};
			}
			return new HYBathroom
			{
				Id = fi.Id.IntegerValue,
				szName = fi.GetParameter("设备名称").AsString(),
				dSupplyFlow = fi.GetParameter("流量").AsDouble(),
				dSupplyEquivalent = fi.GetParameter("CWFU").AsDouble()
			};
		}

		public static HYFittingCal CreateFitting(FamilyInstance fi)
		{
			MechanicalFitting mechanicalFitting = fi.MEPModel as MechanicalFitting;
			if (mechanicalFitting == null)
			{
				return null;
			}
			double value = RevitUtils.GetValue(fi, BuiltInParameter.RBS_LOSS_COEFFICIENT);
			string familyInstanceName = RevitUtils.GetFamilyInstanceName(fi);
			HYFittingCal hyfittingCal = new HYFittingCal();
			switch ((int)mechanicalFitting.PartType)
			{
			case 5:
				hyfittingCal.Type = HYFittingCal.eType.Elbow;
				break;
			case 6:
				hyfittingCal.Type = HYFittingCal.eType.Tee;
				break;
			case 7:
				hyfittingCal.Type = HYFittingCal.eType.Reduce;
				break;
			case 8:
				hyfittingCal.Type = HYFittingCal.eType.Cross;
				break;
			}
			if (hyfittingCal != null)
			{
				hyfittingCal.Id = fi.Id.IntegerValue;
				hyfittingCal.dResCoef = value;
				hyfittingCal.Name = familyInstanceName;
			}
			return hyfittingCal;
		}

		public static HYBathroom CreateBathroom(FamilyInstance fi, Document doc)
		{
			if (fi == null)
			{
				return null;
			}
			FamilySymbol symbol = fi.Symbol;
			HYBathroom hybathroom = new HYBathroom();
			hybathroom.Id = fi.Id.IntegerValue;
			hybathroom.szName = (hybathroom.Name = RevitUtils.GetFamilyInstanceName(fi));
            hybathroom.nOutDN = int.Parse(RevitUtils.GetSharaParameter(fi, "出口直径", (DisplayUnitType)2).ToString());
			SetSanitaryData setSanitaryData = YJKRevitTools.GetSetSanitaryData(doc, fi);
			if (setSanitaryData == null)
			{
				hybathroom.bDefinition = false;
				return hybathroom;
			}
			hybathroom.bDefinition = true;
			hybathroom.dFluxEquivalent = setSanitaryData.DrainEquivalent;
			hybathroom.dFluxFlow = setSanitaryData.DrainFlow;
			hybathroom.dSupplyEquivalent = setSanitaryData.FeedEquivalent;
			hybathroom.dSupplyFlow = setSanitaryData.FeedFlow;
			return hybathroom;
		}

		public static HYFire CreateFire(FamilyInstance fi)
		{
			if (fi == null)
			{
				return null;
			}
			FamilySymbol symbol = fi.Symbol;
			HYFire hyfire = new HYFire();
			hyfire.Id = fi.Id.IntegerValue;
			hyfire.szName = (hyfire.Name = RevitUtils.GetFamilyInstanceName(fi));
			double value;
			if (fi.GetYJKLevel() == null)
			{
				value = (fi.Location as LocationPoint).Point.Z - fi.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM).AsDouble();
			}
			else
			{
				value = fi.GetYJKLevel().Elevation;
			}
			hyfire.dHigh = RevitUtils.GetSharaParameter(fi, "偏移量", 0) + UnitConvert.CovertFromAPI(0, value);
            hyfire.nInDN = int.Parse(RevitUtils.GetSharaParameter(fi, "下进水口直径", (DisplayUnitType)2).ToString());
            hyfire.Flow = RevitUtils.GetSharaParameter(fi, "流量", (DisplayUnitType)66);
			return hyfire;
		}

		public static HYSprinklerCal CreateSprinkler(FamilyInstance fi)
		{
			if (fi == null)
			{
				return null;
			}
			FamilySymbol symbol = fi.Symbol;
			return new HYSprinklerCal
			{
				Id = fi.Id.IntegerValue,
				Name = RevitUtils.GetFamilyInstanceName(fi),
				Location = (fi.Location as LocationPoint).Point,
				Pressure = RevitUtils.GetValue(fi, BuiltInParameter.RBS_PIPE_PRESSUREDROP_PARAM)
			};
		}
	}
}
